package com.xjd.edu.admin.controller;

import com.xjd.edu.admin.domain.SysUserOnline;
import com.xjd.edu.admin.service.ISysUserOnlineService;
import com.xjd.edu.common.CacheConstants;
import com.xjd.edu.common.admin.log.annotation.Log;
import com.xjd.edu.common.admin.log.enums.BusinessType;
import com.xjd.edu.common.admin.security.annotation.RequiresPermissions;
import com.xjd.edu.common.admin.web.page.TableDataInfo;
import com.xjd.edu.common.admin.domain.admin.LoginUser;
import com.xjd.edu.common.utils.StringUtils;
import com.xjd.edu.common.admin.web.controller.BaseController;
import com.xjd.edu.common.admin.domain.AjaxResult;
import com.xjd.edu.toolkit.cache.GameRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 在线用户监控
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/online")
public class SysUserOnlineController extends BaseController {
    @Autowired
    private ISysUserOnlineService userOnlineService;

    //@Autowired
    //private RedisService redisService;

    @Autowired
    GameRedisTemplate gameRedisTemplate;

    @RequiresPermissions("monitor:online:list")
    @GetMapping("/list")
    public TableDataInfo list(String ipaddr, String userName) {
        Collection<String> keys = null;//redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        for (String key : keys) {
            //LoginUser user = redisService.getCacheObject(key);
            //Optional<LoginUser> user = gameRedisTemplate.get(key).orElse(null);
            LoginUser user = null;
            final Optional<LoginUser> opt = gameRedisTemplate.get(key);
            if (opt.isPresent()) {
                user = opt.get();
            }
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
            } else if (StringUtils.isNotEmpty(ipaddr)) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
            } else if (StringUtils.isNotEmpty(userName)) {
                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
            } else {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }

    /**
     * 强退用户
     */
    @RequiresPermissions("monitor:online:forceLogout")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable String tokenId) {
        //redisService.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        gameRedisTemplate.del(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        return success();
    }
}
